package code.tools.method
{
	import code.tools.commponents.Helpers;
	import code.untils.SystemFunction;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 *通用 方法
	 * @author Yuri
	 * 
	 */
	public class CommonMethod
	{
		public function CommonMethod()
		{
		}
		
		/**
		 * 获取通过旋转次数得到建筑物的坐标点索引  逆时针
		 * @param x 格子点
		 * @param z
		 * @param xSize
		 * @param zSize
		 * @param L
		 * @param count 旋转次数
		 * @return 
		 * 
		 */
		public static function getRotationPoint(x:int,z:int,xSize:int,zSize:int,L:int,count:int = 1):Point{
			var p:Point = new Point(x,z);
			switch(count){
				case 1:
				case -3:
					p.x = z;
					p.y = L-(x+xSize);
					break;
				case 2:
				case -2:
					p.x = L-(x+xSize);
					p.y = L-(z+zSize);
					break;
				case 3:
				case -1:
					p.x = L-(z+zSize);
					p.y = x;
					break;
				default:
					break;
			}
			return p;
		}
		
		/**
		 *获取合适的点 
		 * @param x 当前x坐标
		 * @param z 当前y
		 * @param xSize
		 * @param zSize
		 * @param direction
		 * @param orEnterDirection
		 * @param orEnterDistance
		 * @return 
		 * 
		 */
		public static function getRightPoint(x:int,z:int,xSize:int,zSize:int,direction:int,orEnterFaceDirection:int=1,orEnterDistance:int=1,isSupertDirection:int=2):Point{
			var p:Point;
			direction = direction%(direction+1) || 1;
			if(isSupertDirection==2){ //逆时针
				if(direction==2){
					orEnterFaceDirection = orEnterFaceDirection - 1 || 4;
					orEnterDistance = (orEnterFaceDirection==2 || orEnterFaceDirection==4)?zSize-orEnterDistance+1:orEnterDistance;
				}
				x +=  orEnterFaceDirection == 1 || orEnterFaceDirection==3?orEnterDistance:orEnterFaceDirection==2?xSize:1;
				z +=  orEnterFaceDirection == 2 || orEnterFaceDirection==4?orEnterDistance:orEnterFaceDirection==3?zSize:1;
				x--;
				z--;
			}
			p = new Point(x,z);
			return p;
		}
		
		/**
		 *draw bitmapdata 
		 * @param dis
		 * 
		 */
		public static function drawBitmapData(dis:DisplayObject):BitmapData{
			var rectangle:Rectangle = dis.getRect(dis);
			var bWidth:Number = (dis.width==0)?1:dis.width;
			var bHeight:Number =(dis.height==0)?1:dis.height;
			var bitmapD:BitmapData = new BitmapData(bWidth,bHeight,true, 0x00);
			bitmapD.draw(dis,new Matrix(dis.scaleX,0,0,dis.scaleY,-rectangle.x,-rectangle.y));
			return bitmapD;
		}
		
		public static function draw(source:DisplayObject, scale:Number = 1,transparent:Boolean = true, fillColor:uint = 0x00000000):Bitmap{
			var bitmap:Bitmap;
			var rect:Rectangle = source.getBounds(source);
			var x:int = Math.round(rect.x * scale);
			var y:int = Math.round(rect.y * scale);
			
			if (rect.isEmpty())
			{
				rect.width = 1;
				rect.height = 1;
			}
			var bitData:BitmapData = new BitmapData(Math.ceil(rect.width * scale), Math.ceil(rect.height * scale), transparent, fillColor);
			bitData.draw(source, new Matrix(scale, 0, 0, scale, -x, -y), null, null, null, true);
			bitmap   = new Bitmap(bitData);
			bitmap.x = x;
			bitmap.y = y;
			bitmap 	 = clearWhiteColor(bitmap);
			return bitmap;
		}
		
		/**
		 * 清除空白像素 
		 * @param bitData
		 * @return 
		 * 
		 */		
		public static function clearWhiteColor(bitmap:Bitmap):Bitmap{
			var bitData:BitmapData = bitmap.bitmapData; 
			var rect:Rectangle = bitData.getColorBoundsRect(0xFF000000, 0x00000000, false);			
			if (!rect.isEmpty() && (bitData.width != rect.width || bitData.height != rect.height))
			{				
				var realBitData:BitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
				realBitData.copyPixels(bitData, rect, new Point());				
				bitData.dispose();
				bitmap.bitmapData 	 = realBitData;	
				bitmap.x 			+= rect.x;
				bitmap.y 			+= rect.y;
			}	
			return bitmap;
		}
		
		/**
		 *获取限制 
		 * @param obj
		 * @param limW
		 * @param limH
		 * @return 
		 * 
		 */
		public static function getLimtidWH(obj:DisplayObject,limW:Number,limH:Number):void{
			if(limW/limH > obj.width/obj.height){
				obj.width *= limH/obj.height;
				obj.height = limH;
			}else{
				obj.height *= limW/obj.width;
				obj.width = limW;
			}
		}
		
		/**
		 *获取范围内的值 
		 * @param value
		 * @param min
		 * @param max
		 * 
		 */		
		public static function getBound(value:Number,min:Number,max:Number):Number
		{
			return Math.min(max,Math.max(value,min));
		}
		
		public static function setDisplayObjectByStopOneFrame(dis:*):void{
			if(!dis) return;
			if(dis is DisplayObjectContainer)
			{
				var count:int = dis.numChildren;
				for (var i:int = 0; i < count; i++) {
					var c:* = dis.getChildAt(i);
					setDisplayObjectByStopOneFrame(c);
					if (c is MovieClip) {
						c.gotoAndStop(1);
					}
				}
			}
		}
	
		/**
		 *从数组中随机一个索引 
		 * @param arr
		 * @return 
		 * 
		 */
		public static function getRandomIndexFromArray(arr:Array):int{
			return Math.floor(Math.random()*arr.length);
		}
		
		/**
		 * 检测全部为字母
		 * @param str
		 * @return 
		 * 
		 */
		public static function checkIsEnglishWord(str:String):Boolean{
			var r:RegExp = /[^a-z ]/i;
			return !r.test(str);
		}
		
		/**
		 *返回相隔多少天 
		 * @param second1 秒数
		 * @param second2 秒数
		 * @return 
		 * 
		 */		
		public static function getApartDayNum(second1:int,second2:int):int
		{
			var d:int = SystemFunction.countDeltaDay(second2,second1);
			return d + 1;
		}
	}
}